<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript">
    function Tree() {
        let i =0;
        this.root = new Leaf('root');
        function Leaf(data) {
            this.id= i;
            this.data =data;
            this.parent =null;
            this.children =[];
            i=i+1;
        }
        Tree.prototype.append=function (id,data) {
            let newLeaf = new Leaf(data);
            if(id===0){
                newLeaf.parent = this.root;
                this.root.children.push(newLeaf);
            }else{
                let p= this.find(id);
                console.log('p',p)
               newLeaf.parent = p
                p.children.push(newLeaf);
            }
        };
        Tree.prototype.find = id =>  {
            let tree=this.root.children[0];
            function SearchLeaf(tree, id) {
                if (tree.id === id) {
                    return tree;
                }
                else {
                    if (tree.children != null) {
                        tree.children.forEach( item => {
                            let temp = SearchLeaf(item, id);
                            if (temp != null) return temp;
                        })
                    }
                }
                return null;
            }
            return SearchLeaf(tree,id)
        }
    }
    let tree = new Tree();
    console.log(tree);
    console.log(tree.root);
    tree.append(0 ,'000');
    tree.append(1 ,'111');
    tree.append(1 ,'222');
    tree.append(1 ,'333');
    tree.append(2 ,'444');
    tree.append(2 ,'555');
    tree.append(3 ,'333');
    console.log(tree);
    let v=tree.find(7);
    console.log('v:',v)
</script>
</body>
</html>
